/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.startup;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.utils.DateUtil;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.httpservice.HttpGetter;
import cn.ac.iie.di.wx.utils.RedisUtil;
import org.apache.log4j.Logger;

/**
 * 部署节点是ycl@10.136.16.29
 *
 * @author Administrator
 */
public class Starter {

    static Logger logger = Logger.getLogger(Starter.class.getName());

    public static void main(String[] args) {
        try {
            init();
            HttpGetter httpGetter = new HttpGetter();

            String redisHosts = ((String) RuntimeEnv.getParam(RuntimeEnv.REDIS_HOSTS));
            String masterName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_MASTER_NAME);
            String dbName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_DB_NAME);
            RedisUtil msgRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));

            String user = (String) RuntimeEnv.getParam(RuntimeEnv.USER);
            String password = (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD);
            String todayDate = DateUtil.getToDayDate();

            String massAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSALL) + todayDate);//模拟
            String massSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSSUC) + todayDate);
            String singleAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLEALL) + todayDate);
            String singleSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLESUC) + todayDate);
//            logger.info("massAll=" + massAll + "massSuc=" + massSuc + "singleAll=" + singleAll + "singleSuc=" + singleSuc);

            // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
//            String evaluate = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE)+todayDate);
            if (massAll != null && massSuc != null && singleAll != null && singleSuc != null) {
                String msgrequest = "user=" + user + "&password=" + password + "&date=" + todayDate
                        + "&massAll=" + massAll + "&massSuc=" + massSuc + "&singleAll=" + singleAll + "&singleSuc=" + singleSuc;
                String msg_url = (String) RuntimeEnv.getParam(RuntimeEnv.MSG_URL) + "?";
                logger.info(msgrequest);
                httpGetter.doGet(msg_url + msgrequest);
            } else {
                logger.warn("can not get msg datas from redis");
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "serverport.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            throw new Exception("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
        } catch (Exception ex) {
            throw new Exception("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }
}
